import java.util.*;
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room hall, prison,lab, office,mortuary,ward,powerroom,
        underground,lab2,EscapeExit,warehouse,building;
      
        // create the rooms
        hall = new Room("远山精神病院大厅",new Thing("秘制烤肉",60));
        prison = new Room("监狱",new Thing("肾上激素针",50));
        lab = new Room("实验室\n说不定在这里你可以找到装着真相的u盘");
        office = new Room("办公室\n这里说不定有u盘的线索,找找看吧",new Thing("线索:我们在东边建了个实验室专门进行实验，请有关人员注意，如果无许可不得上到二楼实验室",-999));
        mortuary = new Room("停尸间",new Monster("恶灵",45));
        ward = new Room("病房走廊",new Monster("丧尸医生",30));
        powerroom= new Room("电力房",new Monster("电锯恶魔",55));
        underground= new Room("地下监牢\n这是个陷阱，你被电锯恶魔给锁在地下监牢里慢慢折磨致死了\n 很遗憾你并没有找到真相 游戏结束！");
        lab2 = new Room("实验室二楼\n快看！那里有u盘,里面一定记录了整件事件的真相！快拿上它并逃离这里吧！", new Thing("装着证据的U盘",-999));
        EscapeExit = new Room("出口\n快看,那里就是出口,后面的怪物快要追上来了,快从那里离开吧！\n 逃生成功！游戏完成度75%");
        warehouse = new Room("仓库\n这里看来是仓库，一定有食物可以供你补给快去拿吧",new Thing("速食蛋炒饭",50));
        building = new Room("高楼\n这里似乎并没有危险,快进去看看吧说不定能找到补给的东西",new Thing("矿泉水",20));
        
        
        // initialise room exits
        building.setExit("east",hall);
        mortuary.setExit("up",hall);
        building.setExit("up",office);
        building.setExit("down",warehouse);
        warehouse.setExit("up",building);
        hall.setExit("up",ward);
        hall.setExit("down",mortuary);
        hall.setExit("west",building);
        hall.setExit("east",prison);
        hall.setExit("north",powerroom);
        ward.setExit("down",hall);
        office.setExit("down",building);
        prison.setExit("north",lab);
        prison.setExit("west",hall);
        lab.setExit("south",EscapeExit);
        lab.setExit("up",lab2);
        lab2.setExit("down",lab);
        powerroom.setExit("down",underground);
        currentRoom = hall;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
   
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("感谢你的游玩，我们会以更好的游戏再见面的，祝你开心！");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println("欢迎来到逃生游戏,你所扮演的身份是一名记者。");
        System.out.println("你的姓名是迈尔斯 你在某一天接受到一份来自远山精神病院的神秘邮件，里面讲述了有关这所精神所进行的非人道实验，");
        System.out.println("为了调查这个真相你来到了这所精神病院");
        System.out.println("周围寒冷萧条的氛围使你不禁打颤，但是为了找出真相，毅然决然打开了尘封已久的大门");
        System.out.println("你可以输入'help'查看你所需要的指令");
        System.out.println("你当前正处于" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        Thing thing = currentRoom.getThing();

        if(command.isUnknown()) {
            System.out.println("抱歉,该游戏还未支持这个功能");
            return false;
        }

        CommandWord commandWord = command.getCommandWord();
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                wantToQuit = processGo(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case EAT:
                player.eatThing(command);
                break;
            case LOOK:
                look(currentRoom);
                break;
            case PICK:
                player.addToBag(thing);
                currentRoom.remove();
                break;
            case CHECK:
                player.CheckBag();
                break;
            default:
        }
        return wantToQuit;
    }
    
    private boolean processGo(Command command){
    player.walk();
    if(!player.isAlive()){
            System.out.println("你由于体力不支倒下了，很遗憾你没能成功获得真相.游戏结束！");
            return true;
    }
        goRoom(command);
        return false;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("请务必注意，当你的体力归为0的时候，你就会被怪物杀死且无法获得真相。");
        System.out.println("你可以通过以下这些指令来查看");
        System.out.println("你当前的物品和体力值");
        System.out.println("你当前的指令如下:");
        System.out.println("  " + CommandWords.allCommandWords());
    }
    
    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("请加上你所要需要前进的方向");
            return;
        }

        String direction = command.getSecondWord();
        Room nextRoom = currentRoom.goNext(direction);
        if (nextRoom == null) {
            System.out.println("不好意思 这条路被堵了！");
        }
        else {
            currentRoom = nextRoom;
            currentRoom.printInfo();
            if(player.getSpecialItem()&&currentRoom.isEscapeExit()){
                System.out.println("恭喜你，你不仅逃离精神病院还带着证据离开了，相信这个证据一定能够曝光这所病院非人道实验的真相。通关成功！！游戏完成度100%！");
            }
            currentRoom.printExits();
        }
    }
    
    private void look(Room currentRoom) {
        if (currentRoom.getThing() == null) {
            System.out.println("很可惜这里并没有东西可以给你提供补给");
        }
        else System.out.println("有"+currentRoom.getThing().getName());
        if (currentRoom.getMonster() == null) {
            System.out.println("房间安全!");
        }
        else {
            player.hurt(currentRoom.getMonster().getDamage());
            System.out.println("我的天哪! 你被" + currentRoom.getMonster().getMonsterName() +"发现了"+ " !");
            System.out.println("你遭受到了他的攻击");
            System.out.println("你损失了" + currentRoom.getMonster().getDamage() + " 点体力!");
            System.out.println("愣着干嘛？快跑啊，再不走你就要被他所杀死了！! 快点逃离这个房间！");
            currentRoom.removeMonster();
        }
    }
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
